package com.guigu.youxuan.controller;

import com.github.pagehelper.PageInfo;
import com.guigu.youxuan.common.utils.PageUtils;
import com.guigu.youxuan.pojo.OrderInfo;
import com.guigu.youxuan.pojo.dto.OrderInfoDto;
import com.guigu.youxuan.pojo.dto.OrderMakeDto;
import com.guigu.youxuan.pojo.dto.OrderByMidDto;
import com.guigu.youxuan.pojo.dto.OrderMerchantDto;
import com.guigu.youxuan.pojo.dto.SysUnpaidOrderDto;
import com.guigu.youxuan.service.IOrderDetailsService;
import com.guigu.youxuan.service.IOrderInfoService;
import com.guigu.youxuan.common.utils.PageUtils;
import com.guigu.youxuan.common.vo.OrderSearchVo;
import com.guigu.youxuan.pojo.dto.OrderDto;
import com.sun.org.apache.xpath.internal.operations.Or;
import com.guigu.youxuan.utils.NumberUtils;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import java.util.List;
/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liweixin
 * @since 2024-05-15
 */
@CrossOrigin
@Controller
@RequestMapping("/orderInfo")
public class OrderInfoController {
    @Autowired
    private IOrderInfoService iOrderInfoService;

    @Autowired
    private IOrderDetailsService orderDetailsService;

    @Autowired
    private NumberUtils numberUtils;

    /**
     * 商户管理的全部订单查询
     * @param pageNo
     * @param pageSize
     * @return
     */
    @RequestMapping("/allMerchantHubListPage/{pageNo}/{pageSize}/{sysUsersId}")
    @ResponseBody
    public PageInfo<OrderMerchantDto> AllMerchantHubListPage(@PathVariable int pageNo,
                                                             @PathVariable int pageSize,
                                                             @PathVariable String sysUsersId,
                                                             @RequestBody OrderMerchantDto orderMerchantDto){
        return iOrderInfoService.AllMerchantHubListPage(pageNo,pageSize,sysUsersId,orderMerchantDto);
    }

    /**
     * 商户管理的待收货订单查询--P001-3
     * @param pageNo
     * @param pageSize
     * @return
     */
    @RequestMapping("/pendingOrdersListPage/{pageNo}/{pageSize}/{sysUsersId}")
    @ResponseBody
    public PageInfo<OrderMerchantDto> PendingOrdersListPage(@PathVariable int pageNo,
                                                             @PathVariable int pageSize,
                                                             @PathVariable String sysUsersId,
                                                             @RequestBody OrderMerchantDto orderMerchantDto){
        return iOrderInfoService.PendingOrdersListPage(pageNo,pageSize,sysUsersId,orderMerchantDto);
    }

    /**
     * 商户管理待收货订单收货   改变为(P001-7)---待提货
     * @param orderId
     * @return
     */
    @RequestMapping("/shouHuo/{orderId}")
    @ResponseBody
    public String shouHuo(@PathVariable int orderId) {
        try {
            OrderInfo orderInfo = new OrderInfo();
            orderInfo.setConveyTag("P001-7"); // 设置相应的状态
            orderInfo.setOrderId(orderId);
            System.out.println("orderInfo是什么" + orderInfo);
//            System.out.println("收货" + orderInfo.getConveyTag() + "收货");
//            System.out.println("orderId是什么" + orderInfo.getOrderId() + "orderId");
            boolean flag = this.iOrderInfoService.updateById(orderInfo);
            if (flag) {
                return "ok";
            }
        } catch (Exception e) {
            // 打印错误日志
            e.printStackTrace();
            return "error";
        }
        return "error";
    }


    /**
     * 商户管理的待提货订单查询--P001-7的订单
     * @param pageNo
     * @param pageSize
     * @return
     */
    @RequestMapping("/stayTakeOrdersListPage/{pageNo}/{pageSize}/{sysUsersId}")
    @ResponseBody
    public PageInfo<OrderMerchantDto> StayTakeOrdersListPage(@PathVariable int pageNo,
                                                            @PathVariable int pageSize,
                                                            @PathVariable String sysUsersId,
                                                            @RequestBody OrderMerchantDto orderMerchantDto){
        return iOrderInfoService.StayTakeOrdersListPage(pageNo,pageSize,sysUsersId,orderMerchantDto);
    }

    /**
     * 商户管理待提货订单提货  改变为(P001-5)---已完成
     * @param orderId
     * @return
     */
    @RequestMapping("/tiHuo/{orderId}")
    @ResponseBody
    public String tiHuo(@PathVariable int orderId) {
        try {
            OrderInfo orderInfo = new OrderInfo();
            orderInfo.setConveyTag("P001-5"); // P001-5已完成
            orderInfo.setOrderId(orderId);
            System.out.println("orderInfo是什么" + orderInfo);
            System.out.println("提货" + orderInfo.getConveyTag() + "提货");
            System.out.println("orderId是什么" + orderInfo.getOrderId() + "orderId");
            boolean flag = this.iOrderInfoService.updateById(orderInfo);
            if (flag) {
                return "ok";
            }
        } catch (Exception e) {
            // 打印错误日志
            e.printStackTrace();
            return "error";
        }
        return "error";
    }


    /**
     * 商户管理的已提货订单查询--P001-5的订单
     * @param pageNo
     * @param pageSize
     * @return
     */
    @RequestMapping("/alreadyTakeDeliveryOrdersListPage/{pageNo}/{pageSize}/{sysUsersId}")
    @ResponseBody
    public PageInfo<OrderMerchantDto> AlreadyTakeDeliveryOrdersListPage(@PathVariable int pageNo,
                                                             @PathVariable int pageSize,
                                                             @PathVariable String sysUsersId,
                                                             @RequestBody OrderMerchantDto orderMerchantDto){
        return iOrderInfoService.AlreadyTakeDeliveryOrdersListPage(pageNo,pageSize,sysUsersId,orderMerchantDto);
    }



    //改进vvvvv用户个人中心分页查询待支付订单信息
    @RequestMapping("/unpaidOrderListPage2/{sysUsersId}/{pageNo}/{pageSize}")
    @ResponseBody
    public PageUtils<OrderInfoDto> UnpaidOrderListPage2(@PathVariable String sysUsersId,
                                                        @PathVariable int pageNo,
                                                        @PathVariable int pageSize){
        PageUtils<OrderInfoDto> pageUtils = iOrderInfoService.UnpaidOrderListPage2(sysUsersId, pageNo, pageSize);
        return pageUtils;
    }


    /**
     * 通过id查询到要修改用户待支付订单 P001-1 待支付
     * @param orderId
     * @return
     */
    @RequestMapping("/OrderByOrderId/{orderId}")
    @ResponseBody
    public OrderInfo OrderByOrderId(@PathVariable int orderId){
        OrderInfo orderInfo=this.iOrderInfoService.getById(orderId);
        return orderInfo;
    }
    /**
     * 对查到用户待支付id的信息修改 点击支付后配送标志改为 P001-2待发货
     * @param orderInfo
     * @return
     */
    @RequestMapping("/OrderUpdId")
    @ResponseBody
    public String sysUserUpd(@RequestBody OrderInfo orderInfo){
        orderInfo.setConveyTag("P001-2");
        System.out.println("支付"+orderInfo.getConveyTag()+"支付");
        System.out.println("id"+orderInfo.getOrderId()+"id");
        boolean flag=this.iOrderInfoService.updateById(orderInfo);
        if (flag){
            return "ok";
        }
        return "error";
    }

    /**
     * 用户个人中心分页查询待发货订单信息  P001-2
     * @param pageNo
     * @param pageSize
     * @return
     */
    @RequestMapping("/awaitOrderListPage/{pageNo}/{pageSize}/{sysUsersId}")
    @ResponseBody
    public PageInfo<SysUnpaidOrderDto> AwaitOrderListPage(@PathVariable int pageNo,
                                                           @PathVariable int pageSize,
                                                           @PathVariable String sysUsersId){
        return iOrderInfoService.AwaitOrderListPage(sysUsersId,pageNo,pageSize);
    }

    /**
     * 用户个人中心分页查询待提货订单信息  P001-7
     * @param pageNo
     * @param pageSize
     * @return
     */
    @RequestMapping("/awaitPickUpGoods/{pageNo}/{pageSize}/{sysUsersId}")
    @ResponseBody
    public PageInfo<SysUnpaidOrderDto> AwaitPickUpGoods(@PathVariable int pageNo,
                                                @PathVariable int pageSize,
                                                @PathVariable String sysUsersId){
        PageInfo<SysUnpaidOrderDto> page=this.iOrderInfoService.AwaitPickUpGoods(sysUsersId,pageNo,pageSize);
        return page;
    }

    /**
     * 商户管理待提货订单提货  改变为(P001-5)---已完成
     * @param orderId
     * @return
     */
    @RequestMapping("/userTiHuo/{orderId}")
    @ResponseBody
    public String userTiHuo(@PathVariable int orderId) {
        try {
            OrderInfo orderInfo = new OrderInfo();
            orderInfo.setConveyTag("P001-5"); // P001-5已完成
            orderInfo.setOrderId(orderId);
            System.out.println("用户orderInfo是什么" + orderInfo);
            System.out.println("用户提货" + orderInfo.getConveyTag() + "提货");
            System.out.println("用户orderId是什么" + orderInfo.getOrderId() + "orderId");
            boolean flag = this.iOrderInfoService.updateById(orderInfo);
            if (flag) {
                return "ok";
            }
        } catch (Exception e) {
            // 打印错误日志
            e.printStackTrace();
            return "error";
        }
        return "error";
    }

    /**
     * 用户个人中心分页查询已提货订单信息  P001-5
     * @param pageNo
     * @param pageSize
     * @return
     */
    @RequestMapping("/pickUpGoods/{pageNo}/{pageSize}/{sysUsersId}")
    @ResponseBody
    public PageInfo<SysUnpaidOrderDto> pickUpGoods(@PathVariable int pageNo,
                                                        @PathVariable int pageSize,
                                                        @PathVariable String sysUsersId){
        PageInfo<SysUnpaidOrderDto> page=this.iOrderInfoService.PickUpGoods(sysUsersId,pageNo,pageSize);
        return page;
    }



    /**
     * 供应商订单分页查询订单信息
     * @param currentPage
     * @param pageSize
     * @return
     */
    @RequestMapping("/SupplierOrderListPage/{currentPage}/{pageSize}")
    @ResponseBody
    public PageInfo<OrderInfo> SupplierOrderListPage(@PathVariable int currentPage,
                                                     @PathVariable int pageSize
    ){
        PageInfo<OrderInfo> page=this.iOrderInfoService.SupplierOrderListPage(currentPage,pageSize);
        return page;
    }
    /**
     * 供应商根据订单表id查询对应的订单信息
     * @param id
     * @return
     */
    @RequestMapping("/OrderInfoById/{id}")
    @ResponseBody
    public OrderInfo OrderDetailsById(@PathVariable int id){
        OrderInfo goodsInfo=this.iOrderInfoService.getById(id);
        return goodsInfo;
    }

    /**
     * 供应商订单信息 点击发货后配送标志改为 P001-4 待收货
     * @param orderInfo
     * @return
     */
    @RequestMapping("/OrderInfoupdateId")
    @ResponseBody
    public String OrderInfoupdateId(@RequestBody OrderInfo orderInfo){
        orderInfo.setConveyTag("P001-3");
        System.out.println("发货"+orderInfo.getConveyTag()+"发货");
        System.out.println("id"+orderInfo.getOrderId()+"id");
        boolean flag=this.iOrderInfoService.updateById(orderInfo);
        if(flag){
            return "ok";
        }
        return "error";
    }

    /**
     * 订单管理的订单列表
     * @param currentPage
     * @param pageSize
     * @param vo
     * @return
     */
    @RequestMapping("/getOrderList/{currentPage}/{pageSize}")
    @ResponseBody
    public PageUtils<OrderDto> getOrderList(@PathVariable int currentPage,
                                            @PathVariable int pageSize,
                                            @RequestBody OrderSearchVo vo) {
        PageUtils<OrderDto> page = iOrderInfoService.getOrderList(currentPage,pageSize,vo);
        return page;
    }

    @RequestMapping("/createOrder")
    @ResponseBody
    public String createOrder(@RequestBody OrderMakeDto dto) {
        String orderNumber = numberUtils.getTypeNumber("300");
        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setOrderNumber(orderNumber);
        orderInfo.setMerchantId(dto.getOrderInfo().getMerchantId());
        orderInfo.setTotalAmount(dto.getOrderInfo().getTotalAmount());
        orderInfo.setTotalPrice(dto.getOrderInfo().getTotalPrice());
        orderInfo.setSysUsersId(dto.getOrderInfo().getSysUsersId());

        iOrderInfoService.createOrder(dto,orderInfo);

        return "购买成功！";
    }

    /**
     * 根据商户id查询订单
     * @param vo
     * @param currentPage
     * @param pageSize
     * @return
     */
    @RequestMapping("/getOrderByMid/{currentPage}/{pageSize}")
    @ResponseBody
    public PageUtils<OrderByMidDto> getOrderByMid(@RequestBody OrderSearchVo vo,
                                                  @PathVariable int currentPage,
                                                  @PathVariable int pageSize) {
        PageUtils<OrderByMidDto> page = iOrderInfoService.getOrderByMid(currentPage,pageSize,vo);
        return page;
    }
}
